package com.capri.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.capri.R;
import com.capri.base.BaseActivity;
import com.capri.model.INurse;
import com.capri.model.bean.BaseResultBean;
import com.capri.model.bean.VerifyCodeResultBean;
import com.capri.model.impl.NurseModel;
import com.capri.utils.CommonSubscriber;
import com.capri.utils.RxCountDown;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * 忘记密码
 * 第一画面
 */
public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edt_phone_num)
    EditText edtPhoneNum;
    @BindView(R.id.tv_get_validate_code)
    TextView tvGetValidateCode;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;

    private INurse model;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        model = new NurseModel();
    }

    @Override
    public void setupData() {

    }

    @OnClick({R.id.tv_confirm, R.id.tv_get_validate_code})
    public void OnClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_confirm:
                sendValidateCode();
                break;
            case R.id.tv_get_validate_code:
                getValidateCode();
                break;
        }
    }

    private void getValidateCode() {
        if (TextUtils.isEmpty(edtPhoneNum.getText().toString())) {
            Toasty.error(this, "请输入手机号", Toast.LENGTH_SHORT, true).show();
            return;
        }
        model.getVerifyCode(edtPhoneNum.getText().toString()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showDialog();
            }
        }).subscribe(new CommonSubscriber<VerifyCodeResultBean>() {
            @Override
            public void onSuccess(VerifyCodeResultBean response) {
                hideDialog();
                RxCountDown.countdown(60).subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        tvGetValidateCode.setEnabled(true);
                        tvGetValidateCode.setText("获取验证码");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        tvGetValidateCode.setEnabled(false);
                        tvGetValidateCode.setText(integer + " S");
                    }
                });
            }

            @Override
            public void onFailure(BaseResultBean response) {
                hideDialog();
                Toasty.error(ForgetPasswordActivity.this, response.getMsg(), Toast.LENGTH_SHORT, true).show();
            }
        });
    }

    private void sendValidateCode() {
        if (TextUtils.isEmpty(edtPhoneNum.getText().toString())) {
            Toasty.error(this, "请输入手机号", Toast.LENGTH_SHORT, true).show();
            return;
        }
        if (TextUtils.isEmpty(edtPassword.getText().toString())) {
            Toasty.error(this, "请输入验证码", Toast.LENGTH_SHORT, true).show();
            return;
        }
        Intent intent = new Intent(this, SubmitForgetPasswordActivity.class);
        intent.putExtra("key_phone", edtPhoneNum.getText().toString());
        intent.putExtra("key_validate_code", edtPassword.getText().toString());
        startActivity(intent);
    }
}
